home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / MACII.C < prev    next >
Text File  |  1992-07-19  |  7KB  |  469 lines

  1.  
  2. /*
  3.  * Vogle driver for Mac II
  4.  *
  5.  */
  6. #include <stdio.h>
  7.  
  8. #include <quickdraw.h>
  9. #include <qdoffscreen.h>
  10. #include <fcntl.h>
  11. #include <errno.h>
  12. #include <math.h>
  13.  
  14. #include "vogl.h"
  15.  
  16. #define MIN(x,y)    ((x) < (y) ? (x) : (y))
  17. #define COL(c)        ((usememory) ? (c) : (c) | ((c << 4)))
  18.  
  19.  
  20. /****
  21.  * Globals
  22.  ****/
  23. static CWindowPtr        voglWindow;
  24. static Rect                voglRect;
  25. static short            voglWidth;
  26. static short            voglHeight;
  27. static GDHandle            mainGDevice;
  28. static GWorldPtr        voglWorld;
  29. static Boolean            isScreen;
  30. static int                usememory;
  31.  
  32.  
  33. /****
  34.  * InitMacintosh()
  35.  *
  36.  * Initialize all the managers & memory
  37.  *
  38.  ****/
  39.  
  40. void InitMacintosh(void)
  41.  
  42. {
  43.     MaxApplZone();
  44.     
  45.     InitGraf(&thePort);
  46.     InitFonts();
  47.     FlushEvents(everyEvent, 0);
  48.     InitWindows();
  49.     InitMenus();
  50.     TEInit();
  51.     InitDialogs(0L);
  52.     InitCursor();
  53.  
  54. }
  55. /* end InitMacintosh */
  56.  
  57.  
  58. /*
  59.  * redisplay
  60.  *
  61.  *    redisplay the window.
  62.  */
  63. static void
  64. redisplay()
  65. {
  66. }
  67.  
  68.  
  69. /*
  70.  * MacII_init
  71.  *
  72.  *    initialises drawing window
  73.  */
  74. MacII_init()
  75. {
  76.     Rect    *wBounds;
  77.     QDErr    result;
  78.     
  79.     InitMacintosh();
  80.     
  81.     mainGDevice = GetMainDevice();
  82.     voglWidth = 300;
  83.     voglHeight = 300;
  84.  
  85.     SetRect (&voglRect, 50, 50, voglWidth+50, voglHeight+50);
  86.     voglWindow = (CWindowPtr)NewCWindow(0L, &voglRect, "\pVogl Window", true,
  87.                     noGrowDocProc, (WindowPtr) -1L, true, 0);
  88.     ShowWindow (voglWindow);
  89.     BringToFront (voglWindow);
  90.     SetPort (voglWindow);
  91.  
  92.     vdevice.depth = (**(**mainGDevice).gdPMap).pixelSize;
  93.     vdevice.sizeX = vdevice.sizeY = MIN(voglWidth, voglHeight);
  94.     vdevice.sizeSx = voglWidth;
  95.     vdevice.sizeSy = voglHeight;
  96.  
  97.     /* Off screen stuff */
  98.     
  99.     wBounds = &(*((CGrafPtr)voglWindow)->portPixMap)->bounds;
  100.     
  101.     SetOrigin (-wBounds->left, -wBounds->top); /* convert portRect to global */
  102.     
  103.     result = NewGWorld (&voglWorld, 0, &voglWindow->portRect, 0L, 0L, noNewDevice);
  104.     
  105.     SetOrigin (0, 0);
  106.  
  107.     isScreen = TRUE;
  108.  
  109.     /* End of off screen stuff */
  110.  
  111.     return(1);
  112. }
  113.  
  114.  
  115. /*
  116.  * MacII_exit
  117.  *
  118.  *    cleans up before returning the window to normal.
  119.  */
  120. MacII_exit()
  121. {
  122. }
  123.  
  124.  
  125. /*
  126.  * MacII_draw
  127.  *
  128.  *    draws a line from the current graphics position to (x, y).
  129.  *
  130.  * Note: (0, 0) is defined as the top left of the window on a MacII (easy
  131.  * to forget).
  132.  */
  133. MacII_draw(x, y)
  134.     int    x, y;
  135. {
  136.     MoveTo (vdevice.cpVx, vdevice.sizeSy - vdevice.cpVy);
  137.     LineTo (x, vdevice.sizeSy - y);
  138. }
  139.  
  140.  
  141. /*
  142.  * MacII_getkey
  143.  *
  144.  *    grab a character from the keyboard.
  145.  */
  146. int
  147. MacII_getkey()
  148. {
  149.     EventRecord    theEvent;
  150.     Boolean        done;
  151.     int            ch;
  152.  
  153.     done = FALSE;
  154.     
  155.     do {
  156.          if (WaitNextEvent (everyEvent, &theEvent, (short)0, 0L))
  157.          {
  158.             switch (theEvent.what)
  159.             {
  160.                 case keyDown:           /* Handle key inputs, fall into AutoKey code */
  161.                 case autoKey:           /* Handle key inputs */
  162.                     ch = (int)theEvent.message & (int)charCodeMask; /* Get character pressed */
  163.                     done = TRUE;
  164.                     break;              /* End of doing a keystroke */
  165.                 default:
  166.                     break;
  167.             }
  168.          }
  169.      } while (!done);
  170.     return(ch);
  171. }
  172.  
  173.  
  174. /*
  175.  * MacII_checkkey
  176.  *
  177.  *    Check if a keyboard key has been hit. If so return it.
  178.  */
  179.  
  180. int
  181. MacII_checkkey()
  182. {
  183.     EventRecord    theEvent;
  184.     int            ch;
  185.  
  186.      if (WaitNextEvent (everyEvent, &theEvent, (short)0, 0L))
  187.      {
  188.         switch (theEvent.what)
  189.         {
  190.             case keyDown:           /* Handle key inputs, fall into AutoKey code */
  191.             case autoKey:           /* Handle key inputs */
  192.                 ch = (int)theEvent.message & (int)charCodeMask; /* Get character pressed */
  193.                 return(ch);
  194.                 break;              /* End of doing a keystroke */
  195.             default:
  196.                 break;
  197.         }
  198.      }
  199.     return(0);
  200. }
  201.         
  202.  
  203. /*
  204.  * MacII_locator
  205.  *
  206.  *    return the window location of the cursor, plus which mouse button,
  207.  * if any, is been pressed.
  208.  */
  209. int
  210. MacII_locator(wx, wy)
  211.     int    *wx, *wy;
  212. {
  213.     EventRecord    theEvent;
  214.     int            but;
  215.     Point        where;
  216.  
  217.     but = 0;
  218.  
  219.      if (WaitNextEvent (everyEvent, &theEvent, 0, 0L))
  220.      {
  221.         switch (theEvent.what)
  222.         {
  223.             case mouseDown:           /* Handle mouse down */
  224.                 but |= 1;
  225.                 break;              /* End of doing a mouseDown */
  226.             default:
  227.                 break;
  228.         }
  229.      }
  230.      where = theEvent.where;
  231.      GlobalToLocal (&where);
  232.      *wx = where.h;
  233.      *wy = (int)vdevice.sizeSy - where.v;
  234.     return(but);
  235. }
  236.  
  237.  
  238. /*
  239.  * MacII_clear
  240.  *
  241.  *    Clear the screen to current colour
  242.  */
  243. MacII_clear()
  244. {
  245.     EraseRect (&voglWindow->portRect);
  246. }
  247.  
  248.  
  249. /*
  250.  * MacII_color
  251.  *
  252.  *    set the current drawing color index.
  253.  */
  254. MacII_color(ind)
  255.         int    ind;
  256. {
  257. /*
  258.     RGBColor    color;
  259.  
  260.     Index2Color (ind, &color);
  261.     RGBForeColor (&color);
  262. */
  263.     switch (ind)
  264.     {
  265.         case BLACK:
  266.             ForeColor (blackColor);
  267.             break;
  268.         case RED:
  269.             ForeColor (redColor);
  270.             break;
  271.         case GREEN:
  272.             ForeColor (greenColor);
  273.             break;
  274.         case YELLOW:
  275.             ForeColor (yellowColor);
  276.             break;
  277.         case BLUE:
  278.             ForeColor (blueColor);
  279.             break;
  280.         case MAGENTA:
  281.             ForeColor (magentaColor);
  282.             break;
  283.         case CYAN:
  284.             ForeColor (cyanColor);
  285.             break;
  286.         case WHITE:
  287.             ForeColor (whiteColor);
  288.             break;
  289.         default:
  290.             break;
  291.     }
  292. }
  293.  
  294.  
  295. /*
  296.  * MacII_mapcolor
  297.  *
  298.  *    change index i in the color map to the appropriate r, g, b, value.
  299.  */
  300. MacII_mapcolor(i, r, g, b)
  301.     int    i;
  302.     int    r, g, b;
  303. {
  304.     if (i > 15)
  305.         usememory = 1;
  306. }
  307.  
  308.  
  309. /*
  310.  * MacII_font
  311.  *
  312.  *   Set up a hardware font. Return 1 on success 0 otherwise.
  313.  *
  314.  */
  315. MacII_font(fontfile)
  316.         char    *fontfile;
  317. {
  318.     FontInfo fontInfo;
  319.     
  320.     TextFont (monaco);
  321.     GetFontInfo (&fontInfo);
  322.     vdevice.hheight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
  323.     vdevice.hwidth = fontInfo.widMax;
  324.  
  325.     return(1);
  326. }
  327.  
  328.  
  329. /* 
  330.  * MacII_char
  331.  *
  332.  *     outputs one char - is more complicated for other devices
  333.  */
  334. MacII_char(c)
  335.     char    c;
  336. {
  337.     MoveTo (vdevice.cpVx, (int)(vdevice.sizeSy - vdevice.cpVy));
  338.     DrawChar (c);
  339. }
  340.  
  341.  
  342. /*
  343.  * MacII_string
  344.  *
  345.  *    Display a string at the current drawing position.
  346.  */
  347. MacII_string(s)
  348.         char    s[];
  349. {
  350.     MoveTo (vdevice.cpVx, (int)(vdevice.sizeSy - vdevice.cpVy));
  351.     CtoPstr (s);
  352.     DrawString (s);
  353.     PtoCstr (s);
  354. }
  355.  
  356.  
  357. /*
  358.  * MacII_fill
  359.  *
  360.  *    fill a polygon
  361.  */
  362. MacII_fill(n, x, y)
  363.     int    n, x[], y[];
  364. {
  365.     PolyHandle        poly;
  366.     int                i;
  367.  
  368.     if (n > 128)
  369.         verror("vogl: more than 128 points in a polygon");
  370.  
  371.     poly = OpenPoly ();
  372.         MoveTo (x[0], vdevice.sizeSy - y[0]);
  373.         for (i = 1; i < n; i++) {
  374.             LineTo (x[i], vdevice.sizeSy - y[i]);
  375.         }
  376.     ClosePoly ();
  377.     PaintPoly (poly);
  378.     KillPoly (poly);
  379.  
  380.     vdevice.cpVx = x[n-1];
  381.     vdevice.cpVy = y[n-1];
  382. }
  383.  
  384.  
  385. /*
  386.  * MacII_backb
  387.  *
  388.  *    swap to memory only drawing (backbuffer) - a little slow but it
  389.  * works on everything. Where we can, we use the frame buffer.
  390.  */
  391. MacII_backb()
  392. {
  393.     if (voglWorld == 0L)
  394.         return(-1);
  395.  
  396.     if (isScreen == TRUE)
  397.     {
  398.         SetGWorld ((CGrafPtr)voglWorld, 0L);
  399.         isScreen = FALSE;
  400.     }
  401.  
  402.     return(1);
  403. }
  404.  
  405.  
  406. /*
  407.  * MacII_swapb
  408.  *
  409.  *    swap the front and back buffers.
  410.  */
  411. MacII_swapb()
  412. {
  413.     if (voglWorld == 0L)
  414.         return(-1);
  415.         
  416.     SetPort (voglWindow);
  417.     CopyBits ((BitMap*)*voglWorld->portPixMap, (BitMap*)*voglWindow->portPixMap,
  418.                 &voglWindow->portRect, &voglWindow->portRect, srcCopy, 0L);
  419.     SetGWorld ((CGrafPtr)voglWorld, 0L);
  420. }
  421.  
  422.  
  423. /*
  424.  * MacII_frontb
  425.  *
  426.  *    draw in the front buffer
  427.  */
  428. MacII_frontb()
  429. {
  430.     SetPort (voglWindow);
  431.     isScreen = TRUE;
  432. }
  433.  
  434.  
  435. /*
  436.  * the device entry
  437.  */
  438. static DevEntry MacIIdev = {
  439.     "MacII",
  440.     "monaco",
  441.     "chicago",
  442.     MacII_backb,
  443.     MacII_char,
  444.     MacII_checkkey,
  445.     MacII_clear,
  446.     MacII_color,
  447.     MacII_draw,
  448.     MacII_exit,
  449.     MacII_fill,
  450.     MacII_font,
  451.     MacII_frontb,
  452.     MacII_getkey,
  453.     MacII_init,
  454.     MacII_locator,
  455.     MacII_mapcolor,
  456.     MacII_string,
  457.     MacII_swapb
  458. };
  459.  
  460. /*
  461.  * _MacII_devcpy
  462.  *
  463.  *    copy the MacII device into vdevice.dev.
  464.  */
  465. _MacII_devcpy()
  466. {
  467.     vdevice.dev = MacIIdev;
  468. }
  469.